let app = getApp();
// pages/service/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight:0,
    prodAmount: 0,
    GuidId: "",
    Id: 0,
    Name: "",
    HourCost: 0,
    Introduction: "",
    PrdImage: "",
    ProdIds: [],
    Products: [],
    Product: {
      Id: 0,
      Name: "",
      Models: []
    },
    Model: {
      Id: 0,
      Name: "",
      Price: 0,
      IsActive: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        })
      },
    });
    wx.request({
      url:'https://www.limecar.cn/WxApi/ServiceDetail',
      data:{
        id: options.ServiceId
      },
      header:{
        'Content-Type': 'application/json'
      },
      success: function(res){
        console.log(res.data);
        that.setData({
          HourCost: res.data.HourCost,
          prodAmount: res.data.HourCost,
          Id: res.data.Id,
          Introduction: res.data.Introduction,
          Name: res.data.Name,
          Guid: res.data.Guid
        });
        let source = res.data.Products;
        let prodids = [];
        let target = [];
        for (let i = 0; i < source.length; i++){
          let hasProd = false;
          for (let l = 0; l < target.length; l++){
            if (target[l].Name == source[i].ProductName) {
              let modelTemp = {
                Id: source[i].CPTId,
                Name: source[i].Name,
                Price: source[i].Price,
                IsActive: false
              }
              target[l].Models.push(modelTemp);
              hasProd = true;
              break;
            }
          }
          if (!hasProd) {
            let prodTemp = {
              Id: target.length + 1,
              Name: source[i].ProductName,
              Models: []
            };
            let modelTemp = {
              Id: source[i].CPTId,
              Name: source[i].Name,
              Price: source[i].Price,
              IsActive: true
            };
            prodids.push(modelTemp.Id);
            that.setData({
              prodAmount: that.data.prodAmount + modelTemp.Price
            });
            prodTemp.Models.push(modelTemp);
            target.push(prodTemp);
          }
        }
        that.setData({
          Products: target,
          ProdIds: prodids
        })
      }
    });
  },

  radioChange: function(event){
    let that = this;
    let values = event.detail.value.split(';');
    let prodids = that.data.ProdIds;
    let amount = that.data.prodAmount;
    let prod = that.data.Products[values[0]];
    let model = prod.Models[values[1]];
    for(let c = 0; c < prod.Models.length; c++){
      if (prod.Models[c].Id == prodids[values[0]]){
        amount = amount - parseInt(prod.Models[c].Price) + parseInt(model.Price);
        break;
      }
    }
    prodids[values[0]] = model.Id;
    that.setData({
      prodAmount: amount,
      ProdIds: prodids
    })
  },

  continueShop: function(){
    let that= this;
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})